import io
import os
import linecache
import sys

#python aceMesh_trace_convert.py 

def first_line(filename):
   first_line = linecache.getline(filename,1)
   my_list1 =first_line.split(',')
   lines=len(my_list1)
   time=str(int(my_list1[lines-4])+10000).zfill(20)
  
   return '#Paraver (01/01/70 at 00:00):'+time+'_ns:0:1:1'

def trace_line(line):
    
    my_list=line.split(',')
    cpu_id=my_list[1].strip()
    trace_list=[]
    if cpu_id not in cpulist:
      cpulist.append(cpu_id)
    line_id=str(cpulist.index(cpu_id)+1)
    for i in range(0,len(my_list)-1,3):
      time="".join(list(filter(str.isdigit,my_list[i])))
      event_id=my_list[i+2]
      trace_list.append('2:0:1:1:'+line_id+':'+time+':6400017:'+event_id)
    return trace_list

def trace_prv():
   trace_prv=open('./prv/trace.prv','w') 
   firstline=first_line(filename)
   # cpu_=cpu.zfill(20)
   # trace_prv.write(firstline+'('+cpu_+':1)'+'\n')
   #line  =  file.readline()
   while  True:
        # Get next line from file
        line  =  file.readline()
        # If line is empty then end of file reached
        if  not  line  :
           break 
        if len(line.strip()):
           tracelist=trace_line(line)
           for i in range(len(tracelist)):
              trace_prv.write(tracelist[i]+'\n')
   trace_prv.close()
   with open('./prv/trace.prv', 'r+') as f:
        content = f.read()
        f.seek(0, 0)
        cpu_=str(len(cpulist)).zfill(20)
        f.write(firstline+'('+cpu_+':1)'+ '\n' + content)
        f.close()

def trace_pcf():
   trace_pcf=open('./prv/trace.pcf','w')
   trace_pcf.write('DEFAULT_OPTIONS\n\n'+'LEVEL               THREAD\n'+'UNITS               NANOSEC\n'+'LOOK_BACK           100\n'
                  +'SPEED               1\n'+'FLAG_ICONS          ENABLED\n'+'NUM_OF_STATE_COLORS 1000\n'+'YMAX_SCALE          37\n\n\n')
   trace_pcf.write('DEFAULT_SEMANTIC\n\n'+'THREAD_FUNC          State As Is\n\n\n')
   trace_pcf.write('STATES_COLOR\n'+'0   {  0,   0,   0}\n'+'1   {  0,   0, 255}\n'+'2   {217, 217, 217}\n'+'3   {230,  25,  75}\n'
                  +'4   { 60, 180,  75}\n'+'5   {255, 225,  25}\n'+'6   {245, 130,  48}\n'+'7   {145,  30, 180}\n'+'8   { 70, 240, 240}\n'
                  +'9   {240,  50, 230}\n'+'10  {210, 245,  60}\n'+'11  {250, 190, 212}\n'+'12  {  0, 128, 128}\n'+'13  {128, 128, 128}\n'
                  +'14  {  0, 255,   0}\n'+'15  {170, 110,  40}\n'+'16  {255, 250, 200}\n'+'17  {128,   0,   0}\n'+'18  {170, 255, 195}\n'
                  +'19  {128, 128,   0}\n'+'20  {255, 215, 180}\n'+'21  {  0,   0, 128}\n'+'22  { 46, 139,  87}\n'+'23  {  0, 100,   0}\n'
                  +'24  { 47,  79,  79}\n'+'25  {  0, 130, 200}\n\n\n')
   trace_pcf.write('EVENT_TYPE\n'+'0    6400017    Runtime Subsystems\n'+'VALUES\n'+'0     aceMesh:event_end\n'+'1     aceMesh:runtime_init\n'+'2     aceMesh:thread_init\n'
                  +'3     aceMesh:begin_split_task\n'+'4     aceMesh:push_addr\n'+'5     aceMesh:register_task\n'+'6     aceMesh:build_releationgship\n'+'7     aceMesh:set_vertical_task\n'
                  +'8     aceMesh:add_successor\n'+'9     aceMesh:end_split_task\n'+'10     aceMesh:spawn_task\n'+'11     aceMesh:spawn_and_wait\n'+'12     aceMesh:end_construct_dag\n'
                  +'13     aceMesh:wait_for_all_task\n'+'14     aceMesh:execute_stencil_task\n'+'15     aceMesh:execute_noaffinity_task\n'+'16     aceMesh:execute_blocking_task\n'
                  +'17     aceMesh:execute_composite_task\n'+'18     aceMesh:execute_composite_end_task\n'+'19     aceMesh:execute_undefined_task_1\n'+'20     aceMesh:execute_undefined_task_2\n'
                  +'21     aceMesh:undefined_event\n'+'22     aceMesh:execute_task\n'+'23     aceMesh:get_next_task\n'+'24     aceMesh:execute_successor\n'+'25     aceMesh:runtime_shutdown\n\n\n')
   trace_pcf.close()

def trace_row():
   trace_row=open('./prv/trace.row','w')
   trace_row.write('LEVEL NODE SIZE 1\n'+'hostname\n\n'+'LEVEL THREAD SIZE '+str(len(cpulist))+'\n')
   for i in cpulist:
      trace_row.write('CPU '+i+'\n')
   trace_row.close()

cpulist=[]

if __name__ == "__main__":
   
   if not os.path.isdir('./prv'):
     os.system("mkdir prv") 
   filename='./trace.txt'

   file =open(filename)
   trace_prv()  #prv
   trace_pcf()  #pcf
   trace_row()  #row
   file.close()

